﻿using Super.Admin.EFCore;
using Super.Admin.Entities;
using Super.Admin.IServices;

namespace Super.Admin.Services
{
    /// <summary>
    /// 角色服务实现类
    /// </summary>
    public class RoleService : BaseService, IRoleService
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context">操作的数据库上下文</param>
        public RoleService(SuperDataBaseDbContext context) : base(context) { }
        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns>角色实体集合集合</returns>
        public IReadOnlyCollection<RoleEntity> GetRoles()
        {
            return Query<RoleEntity>(r => true).ToList();
        }
        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="roleName">角色名称</param>
        /// <param name="roleId">角色ID</param>
        /// <param name="isActive">状态</param>
        /// <param name="remark">备注</param>
        /// <param name="pageSize">一页数量</param>
        /// <param name="pageIndex">第几页</param>
        /// <param name="totalCount">总数</param>
        /// <returns>权限实体集合集合</returns>
        public IReadOnlyCollection<RoleEntity> GetRoles(string roleName, int roleId, int isActive, string remark, string beginTime, string endTime, int pageSize, int pageIndex, out int totalCount)
        {
            PageResult<RoleEntity> result = QueryPage<RoleEntity, string>(u =>
                           (string.IsNullOrEmpty(roleName) || u.Name.Contains(roleName)) &&
                           (roleId == -1 || u.Id == roleId) &&
                           (isActive == -1 || u.IsActive == isActive) &&
                           (string.IsNullOrEmpty(remark) || u.Remark.Contains(remark)) &&
                           (string.IsNullOrEmpty(beginTime) || u.CreateTime.CompareTo(beginTime) >= 0) &&
                           (string.IsNullOrEmpty(endTime) || u.CreateTime.CompareTo(endTime) <= 0),
                           pageSize, pageIndex, x => x.Id.ToString(), true);
            totalCount = result.TotalCount;
            return result.DataList;
        }
        /// <summary>
        /// 获取权限通过角色ID
        /// </summary>
        /// <param name="roleId">需要权限的角色Id</param>
        /// <returns>权限Ids</returns>
        public IReadOnlyCollection<int> GetPermissionsByRoleId(int roleId)
        {
            List<int> ids = new List<int>();
            List<RolePermissionEntity> roles = Query<RolePermissionEntity>(r => r.RoleId == roleId).ToList();
            foreach (var role in roles)
            {
                ids.Add(role.PermissionId);
            }
            return ids;
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="roleEntity"></param>
        public void CreateRole(RoleEntity roleEntity, IEnumerable<int> permissionIds)
        {
            if (roleEntity == null)
            {
                throw new ArgumentNullException(nameof(roleEntity), "角色不能为空");
            }
            int roleId = Query<RoleEntity>(r => true).Max(x => x.Id) + 1;
            roleEntity.Id = roleId;
            roleEntity.CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            roleEntity.UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            Insert<RoleEntity>(roleEntity);
            foreach (var permissionId in permissionIds)
            {
                Insert<RolePermissionEntity>(new RolePermissionEntity
                {
                    RoleId = roleId,
                    PermissionId = permissionId
                });
            }
            base.Commit(); // 提交事务
        }
        /// <summary>
        /// 修改角色信息
        /// </summary>
        /// <param name="role">需要修改的角色</param>
        public void UpdateRole(RoleEntity roleEntity, IEnumerable<int> permissionIds)
        {
            roleEntity.UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            Update<RoleEntity>(roleEntity);
            List<RolePermissionEntity> list = Query<RolePermissionEntity>(r => r.RoleId == roleEntity.Id).ToList();
            List<object[]> listParam = new List<object[]>();
            foreach (var del in list)
            {
                listParam.Add(new object[] { del.RoleId, del.PermissionId });
            }
            Deletes<RolePermissionEntity>(listParam);
            foreach (var permissionId in permissionIds)
            {
                Insert<RolePermissionEntity>(new RolePermissionEntity
                {
                    RoleId = roleEntity.Id,
                    PermissionId = permissionId
                });
            }
            base.Commit(); // 提交事务
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleId">删除的角色id</param>
        public void DeleteRole(int roleId)
        {
            Delete<RoleEntity>(roleId);
            List<RolePermissionEntity> list = Query<RolePermissionEntity>(r => r.RoleId == roleId).ToList();
            List<object[]> param = new List<object[]>();
            foreach (var item in list)
            {
                param.Add(new object[] { item.RoleId, item.PermissionId });
            }
            Deletes<RolePermissionEntity>(param);
            base.Commit(); // 提交事务
        }
    }
}
